/**
 * 
 */
package com.calf.module.loan.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.calf.cn.aspect.MethodLog;
import com.calf.cn.controller.BaseController;
import com.calf.cn.entity.DataTables;
import com.calf.cn.service.BaseManager;
import com.calf.cn.utils.DateUtil;
import com.calf.cn.utils.SearchUtil;
import com.calf.cn.utils.UUIDUtils;
import com.calf.module.loan.entity.LoanCate;

/**
 * @ClassName LoanCateController
 * @Description Description
 * @author cx
 * @Date 2017年3月10日 下午10:22:29
 * @version 1.0.0
 */
@Controller
@RequestMapping("/loancate")
public class LoanCateController implements BaseController<LoanCate> {

	@Autowired
	private BaseManager baseManager;
	
	/* (非 Javadoc)
	 * Description:
	 * @see com.calf.cn.controller.BaseController#home()
	 */
	@Override
	public String home() {
		return "calfloan/loancate/loancatelist";
	}

	/* (非 Javadoc)
	 * Description:
	 * @see com.calf.cn.controller.BaseController#initTable(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	@RequiresPermissions(value={"loancate:add","loancate:update"},logical=Logical.OR)
	public DataTables<LoanCate> initTable(HttpServletRequest request) {
		Map<String,Object> parameters = SearchUtil.convertorEntitysToMap(request.getParameterMap());
		String sEcho = (String)parameters.get("sEcho");
		List<LoanCate> loanCates = baseManager.query(LoanCate.class, parameters);
		int total = baseManager.get("LoanCate.queryCount", parameters);
		return new DataTables<LoanCate>(sEcho, loanCates, loanCates.size(), total);
	}

	/* (非 Javadoc)
	 * Description:
	 * @see com.calf.cn.controller.BaseController#addAndUpdateHome(org.springframework.ui.Model, java.lang.String)
	 */
	@Override
	@RequiresPermissions(value={"loancate:add","loancate:update"},logical=Logical.OR)
	public String addAndUpdateHome(Model model, String id) {
		if(StringUtils.isNotBlank(id)){
			model.addAttribute("entity", baseManager.get("LoanCate.getLoanCateById", new Object[]{id}));
		}
		return "calfloan/loancate/addLoanCate";
	}

	/* (非 Javadoc)
	 * Description:
	 * @see com.calf.cn.controller.BaseController#saveAdd(java.lang.Object)
	 */
	@Override
	public int saveAdd(LoanCate entity) {
		Subject currentUser = SecurityUtils.getSubject();
		entity.setLoanCateId(UUIDUtils.getUUID());
		entity.setCreateTime(DateUtil.dateFormat());
		entity.setModifyTime(DateUtil.dateFormat());
		entity.setCreateMan(currentUser.getPrincipal().toString());
		entity.setModifyMan(currentUser.getPrincipal().toString());
		return baseManager.insert(entity);
	}

	/* (非 Javadoc)
	 * Description:
	 * @see com.calf.cn.controller.BaseController#delete(java.lang.Long)
	 */
	@Override
	public int delete(Long id) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (非 Javadoc)
	 * Description:
	 * @see com.calf.cn.controller.BaseController#saveUpdate(java.lang.Object)
	 */
	@Override
	public int saveUpdate(LoanCate entity) {
		Subject currentUser = SecurityUtils.getSubject();
		entity.setModifyTime(DateUtil.dateFormat());
		entity.setModifyMan(currentUser.getPrincipal().toString());
		return baseManager.update(entity);
	}

	/* (非 Javadoc)
	 * Description:
	 * @see com.calf.cn.controller.BaseController#delete(java.lang.String)
	 */
	@Override
	@MethodLog(operType="删除")
	@RequiresPermissions(value="loancate:delete")
	public int delete(String id) {
		return baseManager.delete(LoanCate.class, new Object[]{id});
	}

}
